<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>eventhandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_eventhandler.js.html">eventhandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Class to create objects which want to handle multiple events
<a name="line17"></a> * and have their listeners easily cleaned up via a dispose method.
<a name="line18"></a> *
<a name="line19"></a> * Example:
<a name="line20"></a> * &lt;pre&gt;
<a name="line21"></a> * function Something() {
<a name="line22"></a> *   goog.base(this);
<a name="line23"></a> *
<a name="line24"></a> *   ... set up object ...
<a name="line25"></a> *
<a name="line26"></a> *   // Add event listeners
<a name="line27"></a> *   this.listen(this.starEl, goog.events.EventType.CLICK, this.handleStar);
<a name="line28"></a> *   this.listen(this.headerEl, goog.events.EventType.CLICK, this.expand);
<a name="line29"></a> *   this.listen(this.collapseEl, goog.events.EventType.CLICK, this.collapse);
<a name="line30"></a> *   this.listen(this.infoEl, goog.events.EventType.MOUSEOVER, this.showHover);
<a name="line31"></a> *   this.listen(this.infoEl, goog.events.EventType.MOUSEOUT, this.hideHover);
<a name="line32"></a> * }
<a name="line33"></a> * goog.inherits(Something, goog.events.EventHandler);
<a name="line34"></a> *
<a name="line35"></a> * Something.prototype.disposeInternal = function() {
<a name="line36"></a> *   goog.base(this, &#39;disposeInternal&#39;);
<a name="line37"></a> *   goog.dom.removeNode(this.container);
<a name="line38"></a> * };
<a name="line39"></a> *
<a name="line40"></a> *
<a name="line41"></a> * // Then elsewhere:
<a name="line42"></a> *
<a name="line43"></a> * var activeSomething = null;
<a name="line44"></a> * function openSomething() {
<a name="line45"></a> *   activeSomething = new Something();
<a name="line46"></a> * }
<a name="line47"></a> *
<a name="line48"></a> * function closeSomething() {
<a name="line49"></a> *   if (activeSomething) {
<a name="line50"></a> *     activeSomething.dispose();  // Remove event listeners
<a name="line51"></a> *     activeSomething = null;
<a name="line52"></a> *   }
<a name="line53"></a> * }
<a name="line54"></a> * &lt;/pre&gt;
<a name="line55"></a> *
<a name="line56"></a> */
<a name="line57"></a>
<a name="line58"></a>goog.provide(&#39;goog.events.EventHandler&#39;);
<a name="line59"></a>
<a name="line60"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line61"></a>goog.require(&#39;goog.array&#39;);
<a name="line62"></a>goog.require(&#39;goog.events&#39;);
<a name="line63"></a>goog.require(&#39;goog.events.EventWrapper&#39;);
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Super class for objects that want to easily manage a number of event
<a name="line69"></a> * listeners.  It allows a short cut to listen and also provides a quick way
<a name="line70"></a> * to remove all events listeners belonging to this object.
<a name="line71"></a> * @param {Object=} opt_handler Object in whose scope to call the listeners.
<a name="line72"></a> * @constructor
<a name="line73"></a> * @extends {goog.Disposable}
<a name="line74"></a> */
<a name="line75"></a>goog.events.EventHandler = function(opt_handler) {
<a name="line76"></a>  goog.Disposable.call(this);
<a name="line77"></a>  this.handler_ = opt_handler;
<a name="line78"></a>
<a name="line79"></a>  /**
<a name="line80"></a>   * Keys for events that are being listened to.
<a name="line81"></a>   * @type {Array.&lt;number&gt;}
<a name="line82"></a>   * @private
<a name="line83"></a>   */
<a name="line84"></a>  this.keys_ = [];
<a name="line85"></a>};
<a name="line86"></a>goog.inherits(goog.events.EventHandler, goog.Disposable);
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Utility array used to unify the cases of listening for an array of types
<a name="line91"></a> * and listening for a single event, without using recursion or allocating
<a name="line92"></a> * an array each time.
<a name="line93"></a> * @type {Array.&lt;string&gt;}
<a name="line94"></a> * @private
<a name="line95"></a> */
<a name="line96"></a>goog.events.EventHandler.typeArray_ = [];
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Listen to an event on a DOM node or EventTarget.  If the function is omitted
<a name="line101"></a> * then the EventHandler&#39;s handleEvent method will be used.
<a name="line102"></a> * @param {goog.events.EventTarget|EventTarget} src Event source.
<a name="line103"></a> * @param {string|Array.&lt;string&gt;} type Event type to listen for or array of
<a name="line104"></a> *     event types.
<a name="line105"></a> * @param {Function|Object=} opt_fn Optional callback function to be used as the
<a name="line106"></a> *    listener or an object with handleEvent function.
<a name="line107"></a> * @param {boolean=} opt_capture Optional whether to use capture phase.
<a name="line108"></a> * @param {Object=} opt_handler Object in whose scope to call the listener.
<a name="line109"></a> * @return {goog.events.EventHandler} This object, allowing for chaining of
<a name="line110"></a> *     calls.
<a name="line111"></a> */
<a name="line112"></a>goog.events.EventHandler.prototype.listen = function(src, type, opt_fn,
<a name="line113"></a>                                                     opt_capture,
<a name="line114"></a>                                                     opt_handler) {
<a name="line115"></a>  if (!goog.isArray(type)) {
<a name="line116"></a>    goog.events.EventHandler.typeArray_[0] = /** @type {string} */(type);
<a name="line117"></a>    type = goog.events.EventHandler.typeArray_;
<a name="line118"></a>  }
<a name="line119"></a>  for (var i = 0; i &lt; type.length; i++) {
<a name="line120"></a>    // goog.events.listen generates unique keys so we don&#39;t have to check their
<a name="line121"></a>    // presence in the this.keys_ array.
<a name="line122"></a>    var key = /** @type {number} */ (
<a name="line123"></a>        goog.events.listen(src, type[i], opt_fn || this,
<a name="line124"></a>                           opt_capture || false,
<a name="line125"></a>                           opt_handler || this.handler_ || this));
<a name="line126"></a>    this.keys_.push(key);
<a name="line127"></a>  }
<a name="line128"></a>
<a name="line129"></a>  return this;
<a name="line130"></a>};
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Listen to an event on a DOM node or EventTarget.  If the function is omitted
<a name="line135"></a> * then the EventHandler&#39;s handleEvent method will be used. After the event has
<a name="line136"></a> * fired the event listener is removed from the target. If an array of event
<a name="line137"></a> * types is provided, each event type will be listened to once.
<a name="line138"></a> * @param {goog.events.EventTarget|EventTarget} src Event source.
<a name="line139"></a> * @param {string|Array.&lt;string&gt;} type Event type to listen for or array of
<a name="line140"></a> *     event types.
<a name="line141"></a> * @param {Function|Object=} opt_fn Optional callback function to be used as the
<a name="line142"></a> *    listener or an object with handleEvent function.
<a name="line143"></a> * @param {boolean=} opt_capture Optional whether to use capture phase.
<a name="line144"></a> * @param {Object=} opt_handler Object in whose scope to call the listener.
<a name="line145"></a> * @return {goog.events.EventHandler} This object, allowing for chaining of
<a name="line146"></a> *     calls.
<a name="line147"></a> */
<a name="line148"></a>goog.events.EventHandler.prototype.listenOnce = function(src, type, opt_fn,
<a name="line149"></a>                                                         opt_capture,
<a name="line150"></a>                                                         opt_handler) {
<a name="line151"></a>  if (goog.isArray(type)) {
<a name="line152"></a>    for (var i = 0; i &lt; type.length; i++) {
<a name="line153"></a>      this.listenOnce(src, type[i], opt_fn, opt_capture, opt_handler);
<a name="line154"></a>    }
<a name="line155"></a>  } else {
<a name="line156"></a>    var key = /** @type {number} */ (
<a name="line157"></a>        goog.events.listenOnce(src, type, opt_fn || this, opt_capture,
<a name="line158"></a>                               opt_handler || this.handler_ || this));
<a name="line159"></a>    this.keys_.push(key);
<a name="line160"></a>  }
<a name="line161"></a>
<a name="line162"></a>  return this;
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Adds an event listener with a specific event wrapper on a DOM Node or an
<a name="line168"></a> * object that has implemented {@link goog.events.EventTarget}. A listener can
<a name="line169"></a> * only be added once to an object.
<a name="line170"></a> *
<a name="line171"></a> * @param {EventTarget|goog.events.EventTarget} src The node to listen to
<a name="line172"></a> *     events on.
<a name="line173"></a> * @param {goog.events.EventWrapper} wrapper Event wrapper to use.
<a name="line174"></a> * @param {Function|Object} listener Callback method, or an object with a
<a name="line175"></a> *     handleEvent function.
<a name="line176"></a> * @param {boolean=} opt_capt Whether to fire in capture phase (defaults to
<a name="line177"></a> *     false).
<a name="line178"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line179"></a> * @return {goog.events.EventHandler} This object, allowing for chaining of
<a name="line180"></a> *     calls.
<a name="line181"></a> */
<a name="line182"></a>goog.events.EventHandler.prototype.listenWithWrapper = function(src, wrapper,
<a name="line183"></a>    listener, opt_capt, opt_handler) {
<a name="line184"></a>  wrapper.listen(src, listener, opt_capt, opt_handler || this.handler_ || this,
<a name="line185"></a>                 this);
<a name="line186"></a>  return this;
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * @return {number} Number of listeners registered by this handler.
<a name="line192"></a> */
<a name="line193"></a>goog.events.EventHandler.prototype.getListenerCount = function() {
<a name="line194"></a>  return this.keys_.length;
<a name="line195"></a>};
<a name="line196"></a>
<a name="line197"></a>
<a name="line198"></a>/**
<a name="line199"></a> * Unlistens on an event.
<a name="line200"></a> * @param {goog.events.EventTarget|EventTarget} src Event source.
<a name="line201"></a> * @param {string|Array.&lt;string&gt;} type Event type to listen for.
<a name="line202"></a> * @param {Function|Object=} opt_fn Optional callback function to be used as the
<a name="line203"></a> *    listener or an object with handleEvent function.
<a name="line204"></a> * @param {boolean=} opt_capture Optional whether to use capture phase.
<a name="line205"></a> * @param {Object=} opt_handler Object in whose scope to call the listener.
<a name="line206"></a> * @return {goog.events.EventHandler} This object, allowing for chaining of
<a name="line207"></a> *     calls.
<a name="line208"></a> */
<a name="line209"></a>goog.events.EventHandler.prototype.unlisten = function(src, type, opt_fn,
<a name="line210"></a>                                                       opt_capture,
<a name="line211"></a>                                                       opt_handler) {
<a name="line212"></a>  if (goog.isArray(type)) {
<a name="line213"></a>    for (var i = 0; i &lt; type.length; i++) {
<a name="line214"></a>      this.unlisten(src, type[i], opt_fn, opt_capture, opt_handler);
<a name="line215"></a>    }
<a name="line216"></a>  } else {
<a name="line217"></a>    var listener = goog.events.getListener(src, type, opt_fn || this,
<a name="line218"></a>        opt_capture, opt_handler || this.handler_ || this);
<a name="line219"></a>
<a name="line220"></a>    if (listener) {
<a name="line221"></a>      var key = listener.key;
<a name="line222"></a>      goog.events.unlistenByKey(key);
<a name="line223"></a>      goog.array.remove(this.keys_, key);
<a name="line224"></a>    }
<a name="line225"></a>  }
<a name="line226"></a>
<a name="line227"></a>  return this;
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Removes an event listener which was added with listenWithWrapper().
<a name="line233"></a> *
<a name="line234"></a> * @param {EventTarget|goog.events.EventTarget} src The target to stop
<a name="line235"></a> *     listening to events on.
<a name="line236"></a> * @param {goog.events.EventWrapper} wrapper Event wrapper to use.
<a name="line237"></a> * @param {Function|Object} listener The listener function to remove.
<a name="line238"></a> * @param {boolean=} opt_capt In DOM-compliant browsers, this determines
<a name="line239"></a> *     whether the listener is fired during the capture or bubble phase of the
<a name="line240"></a> *     event.
<a name="line241"></a> * @param {Object=} opt_handler Element in whose scope to call the listener.
<a name="line242"></a> * @return {goog.events.EventHandler} This object, allowing for chaining of
<a name="line243"></a> *     calls.
<a name="line244"></a> */
<a name="line245"></a>goog.events.EventHandler.prototype.unlistenWithWrapper = function(src, wrapper,
<a name="line246"></a>    listener, opt_capt, opt_handler) {
<a name="line247"></a>  wrapper.unlisten(src, listener, opt_capt,
<a name="line248"></a>                   opt_handler || this.handler_ || this, this);
<a name="line249"></a>  return this;
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * Unlistens to all events.
<a name="line255"></a> */
<a name="line256"></a>goog.events.EventHandler.prototype.removeAll = function() {
<a name="line257"></a>  goog.array.forEach(this.keys_, goog.events.unlistenByKey);
<a name="line258"></a>  this.keys_.length = 0;
<a name="line259"></a>};
<a name="line260"></a>
<a name="line261"></a>
<a name="line262"></a>/**
<a name="line263"></a> * Disposes of this EventHandler and removes all listeners that it registered.
<a name="line264"></a> * @override
<a name="line265"></a> * @protected
<a name="line266"></a> */
<a name="line267"></a>goog.events.EventHandler.prototype.disposeInternal = function() {
<a name="line268"></a>  goog.events.EventHandler.superClass_.disposeInternal.call(this);
<a name="line269"></a>  this.removeAll();
<a name="line270"></a>};
<a name="line271"></a>
<a name="line272"></a>
<a name="line273"></a>/**
<a name="line274"></a> * Default event handler
<a name="line275"></a> * @param {goog.events.Event} e Event object.
<a name="line276"></a> */
<a name="line277"></a>goog.events.EventHandler.prototype.handleEvent = function(e) {
<a name="line278"></a>  throw Error(&#39;EventHandler.handleEvent not implemented&#39;);
<a name="line279"></a>};
</pre>


</body>
</html>
